/**
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved. 
 */
package com.wilmar.process.core.event;

import java.io.Serializable;
import java.util.Date;

import org.apache.log4j.Logger;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;

import com.wilmar.process.core.services.api.ISystemSubProcessInstanceRelationService;
import com.wilmar.process.entity.ProcessInstanceHelper;
import com.wilmar.process.entity.SubProcessInstanceRelation;
import com.wilmar.process.enumerate.EnumProcessInstanceFixedVariableNameSet;

/**
 * @author makubex Email:makubexnet@gmail.com
 * @version 1.0.0 Jun 17, 2010
 * @since 1.0.0
 */
public class EventListenerSaveSubProcessInstanceRelation implements
		Serializable, EventListener {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -5381393359165347267L;
	private static final Logger log = Logger.getLogger
			(EventListenerSaveSubProcessInstanceRelation.class);

	private ISystemSubProcessInstanceRelationService systemSubProcessInstanceRelationService;

	/**
	 * @see org.jbpm.api.listener.EventListener#notify(org.jbpm.api.listener.EventListenerExecution)
	 */
	public void notify(EventListenerExecution eventListenerExecution)
			throws Exception {
		ProcessInstanceHelper helper = (ProcessInstanceHelper) eventListenerExecution
				.getVariable(EnumProcessInstanceFixedVariableNameSet.PROCESS_INSTANCE_HELPER
						.toString());
		if (helper.getOwnerInstanceExid() == null) {
			helper.setOwnerInstanceExid(eventListenerExecution.getId());
			eventListenerExecution
					.setVariable(
							EnumProcessInstanceFixedVariableNameSet.PROCESS_INSTANCE_HELPER
									.toString(), helper);
			log.debug("It's a main process instance!");
		}
		if (!eventListenerExecution.getId().equals(
				helper.getOwnerInstanceExid())) {
			SubProcessInstanceRelation relation = new SubProcessInstanceRelation();
			relation.setCreateTime(new Date());
			relation.setSubExid(eventListenerExecution.getId());
			relation.setParentExid(helper.getOwnerInstanceExid());
			this.systemSubProcessInstanceRelationService.saveRelation(relation);
			helper.setOwnerInstanceExid(eventListenerExecution.getId());
			eventListenerExecution
					.setVariable(
							EnumProcessInstanceFixedVariableNameSet.PROCESS_INSTANCE_HELPER
									.toString(), helper);
			log
					.debug("It's a sub process instance, process instance helper information updated!");
		} else {
			log.debug("It's a normal process instance!");
		}
	}

	/**
	 * @return the systemSubProcessInstanceRelationService
	 */
	public ISystemSubProcessInstanceRelationService getSystemSubProcessInstanceRelationService() {
		return systemSubProcessInstanceRelationService;
	}

	/**
	 * @param systemSubProcessInstanceRelationService the systemSubProcessInstanceRelationService to set
	 */
	public void setSystemSubProcessInstanceRelationService(
			ISystemSubProcessInstanceRelationService systemSubProcessInstanceRelationService) {
		this.systemSubProcessInstanceRelationService = systemSubProcessInstanceRelationService;
	}
}
